package com.mylg.craw.house.government.category;


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.StringUtils;

/**
 * 全部商品
 */
@Getter
@Setter
@ToString
public class ArticleData {
    private String month;

    //批转预售
    private PreSellStatistics pre;

    //新房总概览统计
    private SellStatisticsGroup newHouse;

    //二手房概览统计
    private SellStatisticsGroup secondHand;

    private String text;

    private boolean hasImages;

    /**
     * 如何分类三段
     *
     * @param text
     * @return
     */
    public static ArticleData parseFrom(String text) {
        if (!StringUtils.hasLength(text)) {
            return null;
        }
        ArticleData articleData = new ArticleData();
        articleData.setMonth(text.substring(0, text.indexOf("月份") + 1));
        articleData.setText(text);
        PreSellStatistics preSellStatistics = PreSellStatistics.parse(text);
        int index = text.indexOf("二手");
        String newHouse = text.substring(preSellStatistics.getEndIndex(), index);
        String secondHand = text.substring(index);
        articleData.setPre(preSellStatistics);
        articleData.setNewHouse(SellStatisticsGroup.parse(newHouse));
        articleData.setSecondHand(SellStatisticsGroup.parse(secondHand));
        return articleData;
    }

    public ExcelOutput convertExcelRow() {
        ExcelOutput output = new ExcelOutput();
        output.setImage(hasImages ? "是" : "否");
        output.setMonth(month);
        output.setText(text);

        if (pre != null) {
            output.setPreviewTotal(pre.getTotal());
            output.setPreviewResidence(pre.getResidence());
            output.setPreviewNoResidence(pre.getNoResidence());
        }

        if (newHouse != null) {
            SellStatistics all = newHouse.getAll();
            if (all != null) {
                output.setNewHouseAllArea(all.getArea());
                output.setNewHouseAllNumber(all.getNumber());
                output.setNewHouseAllAvg(all.getAvgPrice());
            }

            SellStatistics residence = newHouse.getResidence();
            if (residence != null) {
                output.setNewHouseResidenceArea(residence.getArea());
                output.setNewHouseResidenceNumber(residence.getNumber());
                output.setNewHouseResidenceAvg(residence.getAvgPrice());
            }

            SellStatistics noResidence = newHouse.getNoResidence();
            if (noResidence != null) {
                output.setNewHouseNoResidenceArea(noResidence.getArea());
                output.setNewHouseNoResidenceNumber(noResidence.getNumber());
                output.setNewHouseNoResidenceAvg(noResidence.getAvgPrice());
            }
        }

        if (secondHand != null) {
            SellStatistics all = secondHand.getAll();
            if (all != null) {
                output.setSecondHandAllArea(all.getArea());
                output.setSecondHandAllNumber(all.getNumber());
                output.setSecondHandAllAvg(all.getAvgPrice());
            }

            SellStatistics residence = secondHand.getResidence();
            if (residence != null) {
                output.setSecondHandResidenceArea(residence.getArea());
                output.setSecondHandResidenceNumber(residence.getNumber());
                output.setSecondHandResidenceAvg(residence.getAvgPrice());
            }
        }

        return output;
    }
}
